# csv数据用pandas
# 读写json数据用json.dumps()和json.loads()与文件进行交互
# pprint()打印json串，用于打印json以检查数据结构
# import json
#
# datain = {
#     'name': 'ACME',
#     'shares': 100,
#     'price': 542.23
# }
# json_str = json.dumps(datain)
#
# print(json_str)
#
# dataout = json.loads(json_str)
#
# print(dataout)

# from collections import OrderedDict
# import json
#
# s = '{"name": "ACME", "shares": 50, "price": 490.1}'
# datas = json.loads(s, object_pairs_hook=OrderedDict)
# print(datas)
#
#
# class JsonObject:
#     def __init__(self, d):
#         self.__dict__ = d
#
#
# clasdata = json.loads(s, object_hook=JsonObject)
# print(clasdata.name, clasdata.shares, clasdata.price)
#
# print(json.dumps(datas))
# print(json.dumps(datas, indent=4))


# xml.etree.ElementTree.parse() 函数解析整个XML文档并将其转换成一个文档对象
# from urllib.request import urlopen
# from xml.etree.ElementTree import parse
#
# # Download the RSS feed and parse it
# u = urlopen('http://planet.python.org/rss20.xml')
# doc = parse(u)
#
# # Extract and output tags of interest
# for item in doc.iterfind('channel/item'):
#     title = item.findtext('title')
#     date = item.findtext('pubDate')
#     link = item.findtext('link')

# 增量式解析大型xml文件 使用迭代器和生成器，具体情况具体对待
# from xml.etree.ElementTree import iterparse
#
#
# def parse_and_remove(filename, path):
#     path_parts = path.split('/')
#     doc = iterparse(filename, ('start', 'end'))
#     # Skip the root element
#     next(doc)
#
#     tag_stack = []
#     elem_stack = []
#     for event, elem in doc:
#         if event == 'start':
#             tag_stack.append(elem.tag)
#             elem_stack.append(elem)
#         elif event == 'end':
#             if tag_stack == path_parts:
#                 yield elem
#                 elem_stack[-2].remove(elem)
#             try:
#                 tag_stack.pop()
#                 elem_stack.pop()
#             except IndexError:
#                 pass

# 解析和修改xml
# from xml.etree.ElementTree import parse, Element
# import os
#
# filepath = os.path.join('data', 'pred.xml')
# outpath = os.path.join('data', 'newpred.xml')
# doc = parse(filepath)
# root = doc.getroot()
# root.remove(root.find('sri'))
# root.remove(root.find('cr'))
# e = Element('spam')
# e.text = 'This is a test'
# root.insert(2, e)
# doc.write(outpath, xml_declaration=True)
